package com.youmingxing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.youmingxing.R;
import com.youmingxing.adpter.SelectListAdapter;
import com.youmingxing.bean.AuditionDetail;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author evilyin(ChenZhixi)
 * @since 2015-11-20
 */

public class AuditionSelectActivity extends BaseActivity {

    ListView mainListView;
    int movie_id = 0;
    private int auditionStatus = 0;
    private List<AuditionDetail> auditionList = new ArrayList<>();
    private SelectListAdapter adapter;
    public static final String EXTRA_ID = "movie_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_audition_select);
        setTopbarName(R.string.title_activity_audition_select);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            movie_id = bundle.getInt("movie_id");
        }
        mainListView = (ListView) findViewById(R.id.main_select_list_view);
        adapter = new SelectListAdapter(this, auditionList);
        mainListView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        showDialog();
        auditionList.clear();
        AllStarApi.getCategories(movie_id, new BaseJsonHandler(this) {
            @Override
            public void onSuccessWithData(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                JSONArray categories = data.optJSONArray("categories");
                auditionStatus = data.optInt("audition_status");
                if (categories == null) {
                    return;
                }
                for (int i = 0; i < categories.length(); i++) {
                    JSONObject thisCategory = categories.optJSONObject(i);
                    AuditionDetail audition = new AuditionDetail(thisCategory);
                    auditionList.add(audition);
                }
                adapter.notifyDataSetChanged();
                mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent();
                        intent.setClass(AuditionSelectActivity.this, AuditionDetailActivity.class);
                        intent.putExtra("audition_id", auditionList.get(position).cateID);
                        intent.putExtra("movie_id", movie_id);
                        startActivity(intent);
                    }
                });
            }
        });
    }
}